package cn.livingCloud.controller.core;

import cn.livingCloud.dto.*;
import cn.livingCloud.service.MemberService;
import cn.livingCloud.service.PushGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 推送组
 * Created by DingJie on 2016/6/21.
 */
@Controller
@RequestMapping("/admin/report/push_group")
public class PushGroupController {
    @Autowired
    private PushGroupService service;
    @Autowired
    private MemberService memberService;

    @RequestMapping("/list.htm")
    public String list(PushGroupListDto listDto, Model model) {
        service.loadPushGroupListDto(listDto);
        model.addAttribute("listDto", listDto);
        return "report/push_group/list";
    }

    @RequestMapping("/edit.htm")
    public String edit(String uuid, Model model) {
        PushGroupFormDto formDto = service.loadPushGroupFormDto(uuid);
        model.addAttribute("formDto", formDto);
        return "report/push_group/edit";
    }

    @RequestMapping(value = "/edit.htm", method = RequestMethod.POST)
    public String edit(@ModelAttribute("formDto") @Valid PushGroupFormDto formDto, BindingResult result) {
        if (result.hasErrors()) {
            return "report/push_group/edit";
        }
        service.edit(formDto);
        return "redirect:list.htm?edit=success";
    }

    //删除
    @RequestMapping(value = "/delete.htm", method = RequestMethod.POST)
    public String delete(String uuid, Model model) {
        service.delete(uuid);
        model.addAttribute("delete", "success");
        return "redirect:list.htm";
    }

    //状态更改
    @RequestMapping("/change_state")
    public String changeState(String uuid, boolean state, Model model) {
        PushGroupDto pushGroupDto = service.uploadState(uuid, state);
        model.addAttribute("listDto", pushGroupDto);
        return "report/push_group/list_item";
    }

    //分组下的粉丝列表
    @RequestMapping("/member_list.htm")
    public String memberList(MemberByPushGroupUuidListDto listDto, Model model) {
        service.loadMemberByPushGroupUuidListDto(listDto);
        model.addAttribute("listDto", listDto);
        return "report/push_group/member_list";
    }

    /**
     * 添加粉丝微信号到分组 界面
     */
    @RequestMapping("/bind_member.htm")
    public String bindMember(@RequestParam("pushGroupUuid") String pushGroupUuid, Model model) {
        PushGroupBindMemberDto formDto = service.loadBindMemberDto(pushGroupUuid);
        model.addAttribute("formDto", formDto);
        return "report/push_group/bind_member";
    }

    /**
     * 添加粉丝微信号到分组 提交
     */
    @RequestMapping(value = "/bind_member.htm", method = RequestMethod.POST)
    public String bindMemberSubmit(@ModelAttribute("formDto") @Valid PushGroupBindMemberDto formDto, BindingResult result, Model model) {
        if (result.hasErrors()) {
            return "report/push_group/bind_member";
        }
        service.bindMember(formDto);
        model.addAttribute("pushGroupUuid", formDto.getPushGroupUuid());
        return "redirect:/admin/report/push_group/bind_member.htm";
    }

    /**
     * 搜索相关会员
     */
    @RequestMapping("/load_member_json")
    @ResponseBody
    public MemberListDto searchMember(String key, Model model) {
        return memberService.findUnbindMember(key);
    }

}



